/***************************************************************************************************
*                                                                                                  *
* Author: Ivan Antonov (ivan.antonov@gatech.edu)                                                   *
*                                                                                                  *
***************************************************************************************************/

// $Id: params.cpp 1936 2012-08-31 18:14:53Z antonov $

#include <stdio.h>      // printf() , fopen()  , fclose()
#include <stdlib.h>     // atoi()   , exit()

#include "params.h"
#include "getopt.h"

/***************************************************************************************************
*                                                                                                  *
* Params() -- constructor of the Params class                                                      *
*                                                                                                  *
* Arguments:                                                                                       *
*     int   argc   -- number of command line arguments for the program                             *
*     char* argv[] -- array of command line arguments for the program                              *
*                                                                                                  *
***************************************************************************************************/
Params::Params(const int argc, char* argv[])
{
        this->init();

        this->command_line = "";
        for(int i = 0; i < argc; ++i)
        {
                if(i > 0)
                        this->command_line += " ";
                this->command_line.append( argv[i] );
        }

        GetOpt getopt(argc, argv, "s:m:g:");

        int c;
        while((c = getopt.next_opt()) != EOF) 
        {
                switch (c)
        {  
                case 's':
                                this->seq_fn.assign( getopt.optarg );
                                break;
                case 'm':
                                this->model_fn.assign( getopt.optarg );
                                break;
                case 'g':
                                this->prediction_fn.assign( getopt.optarg );
                                break;
                }
        }

		if(this->seq_fn.length() == 0 || this->model_fn.length() == 0 || this->prediction_fn.length() == 0)
        {
                this->usage();
				cin.get();
                exit(0);
        }
}

void Params::init()
{
        this->display_name        = "PostDecoding";
        this->prg_name            = "genestart";
        this->version             = "0";
        this->descr               = "";
        this->parse_svn_str("$Id: params.cpp 1936 2012-12-5 18:14:53Z gena $");
}

void Params::parse_svn_str(const string svn_str)
{
        int start, end;

        // Derive svn_revision
        start = (int)svn_str.find(" ", 5) + 1;
        end   = (int)svn_str.find(" ", start);
        this->svn_revision = atoi( svn_str.substr(start, end-start).c_str() );

        // Derive svn_date
        start = end+1;
        end   = (int)svn_str.find(" ", start);
        this->svn_date = svn_str.substr(start, end-start);
}

/***************************************************************************************************
*                                                                                                  *
* usage() -- prints usage                                          
*                                                                                                  *
***************************************************************************************************/
void Params::usage()
{
        cerr << "\n"+display_name+", version "+version+", revision " << svn_revision << " ("+svn_date+")\n";
        if( !this->descr.empty() )
                cerr << "\n"+this->descr+"\n";
        cerr << "\n";
        cerr << "OPTIONS:\n";
        cerr << "\n";
        cerr << "USAGE:\n";
        cerr << "    "+prg_name+" [OPTIONS] -m genemarks_model_file -s SEQUENCE.fasta -g genefile.lst > OUT.txt\n";
        cerr << "\n";
}
